function divergence = compute_cluster_divergence (clusterA, clusterB)


% NOTE !
%
% This function is often called unnecessarily in the main experiment
% scripts, and its results should be stored for later re-use.



divergence = inf;

if (isempty(clusterA) || isempty(clusterB)),
  % divergence is infinite if (at least) one of the clusters is empty
  return;
end

% compute highest score of each MoG in the smaller cluster
% w.r.t. any MoG in the bigger cluster

divergence = 0;

clusterBig = clusterA;
clusterSmall = clusterB;

if length(clusterA) < length(clusterB),
  clusterBig = clusterB;
  clusterSmall = clusterA;
end

sizeBig = length(clusterBig);
sizeSmall = length(clusterSmall);

for ids=1:sizeSmall,
  minDiv = inf;
  for idb=1:sizeBig,
        
    mogS = clusterSmall{ids};
    mogB = clusterBig{idb};

    curDiv = gm_compare_jmef(mogS,mogB,0);

    if curDiv < minDiv,
      minDiv = curDiv;
    end
  end
    
  divergence = divergence + minDiv;
end

divergence = divergence / sizeSmall;
